require(File.join(File.dirname(__FILE__), '../../../config', 'boot'))

require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'


class Dir
  def copy_to(dest_dir)
    src_dir = Pathname.new(self.path)
    dest_dir = Pathname.new(dest_dir)
    FileList["#{src_dir}/**/*"].each do |src|
      next if File.directory?(src)
      dest = dest_dir + Pathname.new(src).relative_path_from(src_dir)
      mkdir_p dest.dirname.to_s unless dest.dirname.directory?
      cp(src, dest) unless uptodate?(dest, src)
    end
  end
end

fake_root_cmd = 'fakeroot'
fake_root_path = 'fakeroot'
tmp_root_path = "#{fake_root_path}/tmp"
gems_copy_path = "#{tmp_root_path}/tmp/rubygems"

###################### Note ######################### 
# change application specific path and filenames here
app_path = "../../.."
app_copy_path = "#{tmp_root_path}/usr/share/RailsSampleApp"
deb_file_name = 'rails-sample_0.0.1_all.deb'
rpm_file_name = 'rails-sample-0.0.1-2.noarch.rpm'

task :deb_clean do
	cd(fake_root_path)
	system(fake_root_cmd, 'debian/rules', 'clean')
end

task :deb_pak do
	rm_f(deb_file_name)
	rm_f(rpm_file_name)
  	Dir.new(app_path).copy_to(app_copy_path)
	Dir.new('rubygems').copy_to(gems_copy_path)
	cd(fake_root_path)
	system(fake_root_cmd, 'dpkg-buildpackage')
	cd('..')
	system('alien', '-r', deb_file_name, '--scripts')
	rm_rf(tmp_root_path)

# 	for test
	rm_f("#{app_path}/public/#{deb_file_name}")
	cp(deb_file_name, '#{app_path}/public/')
end
